package com.hemerasolutions.v2trainer.model.exercise;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;

import com.hemerasolutions.v2trainer.model.base.BaseModel;
import com.hemerasolutions.v2trainer.model.equipment.VersionableEquipment;
import com.hemerasolutions.v2trainer.model.multimedia.Photo;
import com.hemerasolutions.v2trainer.model.multimedia.Video;
import com.hemerasolutions.v2trainer.model.users.User;
import com.hemerasolutions.v2trainer.util.Utility;

@Entity
public class Equipment extends BaseModel {
 
	private static final long serialVersionUID = 1L;

    @Column(length=50)
	private String name;

    @Column(length=180)
	private String description;

    @Column(length=180)
	private String obs;

    @OneToMany(cascade=CascadeType.ALL,orphanRemoval=true)
	private List<Photo> photos;

    @OneToMany(cascade=CascadeType.ALL,orphanRemoval=true)
	private List<Video> videos;

    @ManyToOne
    private VersionableEquipment versionableEquipment;
    
    @Column
	private boolean active;

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
	private Date createdAt;

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
	private Date lastChange;

	@OneToOne 
    private User changedBy;

	
	public Equipment() {
		super();
		this.photos 	= new ArrayList<Photo>();
		this.videos 	= new ArrayList<Video>();
		this.createdAt	= Utility.getRoundedDate();
		this.lastChange	= Utility.getRoundedDate();
		this.active		= true;
		
	}
	
	

	public Equipment(Equipment other) {
		this();
		this.name = other.name;
		this.description = other.description;
		this.obs = other.obs;
		this.active = other.active;
		this.createdAt = other.createdAt;
		this.lastChange = other.lastChange;
		this.changedBy = other.changedBy;
		this.versionableEquipment = other.versionableEquipment;

		for(Photo photo:other.getPhotos()){
			this.photos.add(new Photo(photo));
		}
		
		for(Video video:other.getVideos()){
			this.videos.add(new Video(video));
		}
	}


	public Equipment(Equipment other,VersionableEquipment versionableEquipment) {
		super();
		
		this.name = other.name;
		this.description = other.description;
		this.obs = other.obs;
		this.active = other.active;
		this.createdAt = other.createdAt;
		this.lastChange = other.lastChange;
		this.changedBy = other.changedBy;
		this.versionableEquipment = versionableEquipment;
		
		for(Photo photo:other.getPhotos()){
			this.photos.add(new Photo(photo));
		}
		
		for(Video video:other.getVideos()){
			this.videos.add(new Video(video));
		}
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getName() {
		return name;
	}

	public String getDescription() {
		return description;
	}

	public String getObs() {
		return obs;
	}

	public List<Photo> getPhotos() {
		return photos;
	}

	public List<Video> getVideos() {
		return videos;
	}

	public boolean isActive() {
		return active;
	}

	public Date getCreatedAt() {
		return createdAt;
	}

	public Date getLastChange() {
		return lastChange;
	}


	public User getChangedBy() {
		return changedBy;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setObs(String obs) {
		this.obs = obs;
	}

	public void setPhotos(List<Photo> photos) {
		this.photos = photos;
	}

	public void setVideos(List<Video> videos) {
		this.videos = videos;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}

	public void setLastChange(Date lastChange) {
		this.lastChange = lastChange;
	}

	public void setChangedBy(User changedBy) {
		this.changedBy = changedBy;
	}

	public VersionableEquipment getVersionableEquipment() {
		return versionableEquipment;
	}

	public void setVersionableEquipment(VersionableEquipment versionableEquipment) {
		this.versionableEquipment = versionableEquipment;
	}

	public Photo getPrincipalPhoto(){
		Photo photo = null;
		if(this.photos != null && !this.photos.isEmpty()){
			photo = this.photos.get(0);
			for(Photo photoAux:this.photos){
				if(photoAux.isPrincipal()){
					photo = photoAux;
					break;
				}
			}
		}
		return photo;
	}

	public Video getPrincipalVideo(){
		Video video = null;
		if(this.videos != null && !this.videos.isEmpty()){
			video = this.videos.get(0);
			for(Video videoAux:this.videos){
				if(videoAux.isPrincipal()){
					video = videoAux ;
				}
			}
		}
		return video;
	}



	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (active ? 1231 : 1237);
		result = prime * result
				+ ((changedBy == null) ? 0 : changedBy.hashCode());
		result = prime * result
				+ ((createdAt == null) ? 0 : createdAt.hashCode());
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result
				+ ((lastChange == null) ? 0 : lastChange.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((obs == null) ? 0 : obs.hashCode());
		result = prime
				* result
				+ ((versionableEquipment == null) ? 0 : versionableEquipment
						.hashCode());
		return result;
	}



	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Equipment other = (Equipment) obj;
		if (active != other.active) {
			return false;
		}
		if (changedBy == null) {
			if (other.changedBy != null) {
				return false;
			}
		} else if (!changedBy.equals(other.changedBy)) {
			return false;
		}
		if (createdAt == null) {
			if (other.createdAt != null) {
				return false;
			}
		} else if (!createdAt.equals(other.createdAt)) {
			return false;
		}
		if (description == null) {
			if (other.description != null) {
				return false;
			}
		} else if (!description.equals(other.description)) {
			return false;
		}
		if (id == null) {
			if (other.id != null) {
				return false;
			}
		} else if (!id.equals(other.id)) {
			return false;
		}
		if (lastChange == null) {
			if (other.lastChange != null) {
				return false;
			}
		} else if (!lastChange.equals(other.lastChange)) {
			return false;
		}
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		if (obs == null) {
			if (other.obs != null) {
				return false;
			}
		} else if (!obs.equals(other.obs)) {
			return false;
		}
		if (versionableEquipment == null) {
			if (other.versionableEquipment != null) {
				return false;
			}
		} else if (!versionableEquipment.equals(other.versionableEquipment)) {
			return false;
		}
		return true;
	}



	@Override
	public String toString() {
		return "Equipment [id=" + id + ", name=" + name + ", description="
				+ description + ", obs=" + obs + ", versionableEquipment="
				+ versionableEquipment + ", active=" + active + ", createdAt="
				+ createdAt + ", lastChange=" + lastChange + ", changedBy="
				+ changedBy + "]";
	}
	
}
 
